<?php
namespace app\admin\controller;
use think\Controller;
//use app\admin\model\Category;
use think\Session;
use think\Validate;
use think\Db;
class Companys extends Controller
{
    //显示公司详情列表
	public function companyList()
	{
        $name = input('post.name');
        if(empty($name)){
            $data = db::name('company_information')->where(true)->paginate(4);
        }else{
            $data = Db::name('company_information')->where('company_leader','like',"%$name%")->paginate(4);
        }
        $count = Db::name('company_information')->count();

        $this->assign('list',$data);
        $this->assign('count',$count);
        return $this->fetch('company/company-list');
	}
//

//显示添加
    public function addCompany()
    {

        return view('company/company-add');
    }

//执行添加详细信息操作
    public function add()
    {
        $data = input('post.');

//        dump($data);die;

        //获取表单上传的文件
        $file = request()->file('company_icon');
        //移动到框架应用根目录下/public/uploads/目录下
        if($file){
            $info = $file->validate(['size'=>2097152,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                //获取图片格式 .jpg
//                echo $info->getExtension().'<br>';
                //得到图片路径地址
//                echo $info->getSaveName().'<br>';
                //得到图片的地址
//                echo $info->getFilename().'<br>';

//                dump($path);die;
                $savename = $info->getSaveName();
                $data['company_icon'] = str_replace('\\','/',$savename);
                $data['company_icon'] = '/uploads/'.$data['company_icon'];
            }else{
                return $file->getError();

            }
        }


        if (!$data['company_pwd'] == $data['company_repwd'] ){
            return $this->error('两次密码不一致!');
        }

///       dump($data);die;
        $validate = new Validate([
            'company_pwd'  => 'require|min:2',
            'company_repwd'  => 'require|min:2',
            'company_username' =>['regex'=>'/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/'],
            'company_name'  => 'require|min:2',
            'company_leader'  => 'require|min:2',
            'company_qq'  => 'require|max:20',
            'company_address'  => 'require|min:2',
            'company_intro'  => 'require|min:2',
            'company_email'  => ['regex'=>'/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/'],
            'company_phone'  => 'require|number',
            'company_tel' => ['regex'=>'/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/']
        ]);


        $data['company_pwd'] = md5($data['company_pwd']);

        $data['company_regtime'] = time();
        $data['company_update'] = time();

        $list['company_tel'] = $data['company_username'];
        $list['company_pwd'] = $data['company_pwd'];
        $list['company_regtime'] = $data['company_regtime'];

        if (!$validate->check($data)) {
            $arr = ['code'=>0,'msg'=>$validate->getError()];
        }else{
            $username = Db::name('company')->where(['company_tel'=>$list['company_tel']])->find();
            if($username){
                $arr = ['code'=>0,'msg'=>'企业用户存在!'];
            }else{

                $rowsid = Db::name('company')->insertGetId($list);
                $newdata['company_cid'] = $rowsid;
                $newdata['company_icon'] = $data['company_icon'];
                $newdata['company_name'] = $data['company_name'];
                $newdata['company_leader'] = $data['company_leader'];
                $newdata['company_tele'] = $data['company_tele'];
                $newdata['company_phone'] = $data['company_phone'];
                $newdata['company_qq'] = $data['company_qq'];
                $newdata['company_email'] = $data['company_email'];
                $newdata['company_address'] = $data['company_address'];
                $newdata['company_intro'] = $data['company_intro'];
                $newdata['company_update'] = $data['company_update'];

                $lastInsertId = Db::name('company_information')->insertGetId($newdata);
                if($lastInsertId){

                    $arr = ['code'=>0,'msg'=>'添加成功!'];
                }else{
                    $arr = ['code'=>0,'msg'=>'添加失败!'];
                }
            }
        }

        if ($arr['code'] == 0) {
            return $this->error($arr['msg']);
        } else {
            return $this->success($arr['msg'],url('admin/Companys/companyList'));
        }

    }



    //修改禁用和启用
    public function changeStatus1($id)
    {
        Db::name('company_information')->where('company_id', $id)
            ->update(['company_status' => '2']);
        return $id;
    }

    public function changeStatus2($id)
    {
        Db::name('company_information')->where('company_id', $id)
            ->update(['company_status' => '1']);
        return $id;
    }

    //删除企业信息操作
    public function delCompany($id)
    {
        $result = Db::name('company_information')->delete($id);
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的企业信息删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的企业信息删除失败!';
        }

        return json($info);
    }


    //企业编辑页面
    public function editList($id)
    {
        $data = Db::name('company_information')->find($id);

        $this->assign('list',$data);
        return $this->fetch('company/company-edit');
    }

    //执行编辑操作
    public function Edit($id)
    {
        $data = input('post.');

        //获取表单上传的文件
        $file = request()->file('company_icon');
        //移动到框架应用根目录下/public/uploads/目录下
        if($file){
            $info = $file->validate(['size'=>2097152,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){

                $savename = $info->getSaveName();
                $data['company_icon'] = str_replace('\\','/',$savename);
                $data['company_icon'] = '/uploads/'.$data['company_icon'];
            }else{
                return $file->getError();

            }
        }

        $validate = new Validate([
            'company_name'  => 'require|min:2',
            'company_leader'  => 'require|min:2',
            'company_qq'  => 'require|max:20',
            'company_address'  => 'require|min:2',
            'company_intro'  => 'require|min:2',
            'company_email'  => ['regex'=>'/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/'],
            'company_phone'  => 'require|number',
            'company_tele' => ['regex'=>'/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/']
        ]);

        $data['company_update'] = time();

        if (!$validate->check($data)) {
            $arr = ['code'=>0,'msg'=>$validate->getError()];
        }else{
            $username = Db::name('company_information')->where(['company_name'=>$data['company_name']])->find();
            if($username){
                $arr = ['code'=>0,'msg'=>'企业用户存在!'];
            }else{

                $rows = Db::name('company_information')->where('company_id',$id)->update($data);

                if($rows){
                    $arr = ['code'=>0,'msg'=>'修改成功!'];
                }else{
                    $arr = ['code'=>0,'msg'=>'修改失败!'];
                }
            }
        }

        if ($arr['code'] == 0) {
            return $this->error($arr['msg']);
        } else {
            return $this->success($arr['msg'],url('admin/Companys/companyList'));
        }

    }


    //企业具体详情信息
    public function detailList($id)
    {
        $data = Db::name('company_information')->find($id);

        $this->assign('list',$data);
        return $this->fetch('company/company-detail');
    }





}